<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件编辑器</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f5f7fa;
            padding: 20px;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        
        .container {
            width: 100%;
            max-width: 900px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            padding: 25px;
            margin: 20px 0;
        }
        
        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 20px;
            font-size: 1.8rem;
        }
        
        .description {
            text-align: center;
            margin-bottom: 25px;
            color: #7f8c8d;
            font-size: 1rem;
        }
        
        .file-input-section {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-bottom: 20px;
            padding: 15px;
            background-color: #f8f9fa;
            border-radius: 8px;
        }
        
        .file-input-label {
            display: inline-block;
            padding: 12px 25px;
            background-color: #3498db;
            color: white;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s;
            font-weight: 500;
            margin-bottom: 10px;
            text-align: center;
        }
        
        .file-input-label:hover {
            background-color: #2980b9;
        }
        
        #fileInput {
            display: none;
        }
        
        .file-name {
            font-size: 0.9rem;
            color: #7f8c8d;
            margin-top: 5px;
        }
        
        .editor-section {
            margin-bottom: 20px;
        }
        
        textarea {
            width: 100%;
            min-height: 300px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-family: 'Courier New', monospace;
            font-size: 14px;
            resize: vertical;
            line-height: 1.5;
        }
        
        .button-section {
            display: flex;
            justify-content: center;
            gap: 15px;
            margin-top: 20px;
        }
        
        button {
            padding: 12px 30px;
            border: none;
            border-radius: 5px;
            font-size: 1rem;
            cursor: pointer;
            transition: all 0.3s;
            font-weight: 500;
        }
        
        #saveButton {
            background-color: #2ecc71;
            color: white;
        }
        
        #saveButton:hover:not(:disabled) {
            background-color: #27ae60;
        }
        
        #resetButton {
            background-color: #e74c3c;
            color: white;
        }
        
        #resetButton:hover {
            background-color: #c0392b;
        }
        
        button:disabled {
            background-color: #bdc3c7;
            cursor: not-allowed;
        }
        
        .status-message {
            text-align: center;
            margin-top: 15px;
            padding: 10px;
            border-radius: 5px;
            display: none;
        }
        
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        /* 横屏优化 */
        @media (orientation: landscape) {
            .container {
                max-width: 95%;
            }
            
            textarea {
                min-height: 400px;
            }
            
            .file-input-section {
                flex-direction: row;
                justify-content: center;
                gap: 20px;
            }
            
            .file-name {
                margin-top: 0;
            }
        }
        
        /* 竖屏优化 */
        @media (orientation: portrait) {
            .container {
                padding: 15px;
            }
            
            h1 {
                font-size: 1.5rem;
            }
            
            textarea {
                min-height: 350px;
            }
        }
        
        /* 小屏幕优化 */
        @media (max-width: 600px) {
            body {
                padding: 10px;
            }
            
            .container {
                padding: 15px;
            }
            
            .button-section {
                flex-direction: column;
            }
            
            button {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>文件编辑器</h1>
        <p class="description">选择文件进行编辑，修改完成后保存到本地</p>
        
        <div class="file-input-section">
            <label for="fileInput" class="file-input-label">选择文件</label>
            <div class="file-name" id="fileName">未选择文件</div>
        </div>
        <input type="file" id="fileInput" accept=".txt,.js,.css,.html,.json,.md,.xml,.csv">
        
        <div class="editor-section">
            <textarea id="fileContent" placeholder="文件内容将显示在这里..."></textarea>
        </div>
        
        <div class="button-section">
            <button id="saveButton" disabled>保存文件</button>
            <button id="resetButton">重置内容</button>
        </div>
        
        <div id="statusMessage" class="status-message"></div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const fileInput = document.getElementById('fileInput');
            const fileContent = document.getElementById('fileContent');
            const saveButton = document.getElementById('saveButton');
            const resetButton = document.getElementById('resetButton');
            const fileNameDisplay = document.getElementById('fileName');
            const statusMessage = document.getElementById('statusMessage');

            const translation = [];
            
            let currentFile = null;
            
            // 文件选择事件
            fileInput.addEventListener('change', function(e) {
                const file = e.target.files[0];
                if (file) {
                    currentFile = file;
                    fileNameDisplay.textContent = file.name;
                    
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        const prompts = JSON.parse(e.target.result).data.prompts;
                        fileContent.value = JSON.stringify(prompts, null, 2);
                        saveButton.disabled = false;
                        hideStatusMessage();
                    };
                    
                    reader.onerror = function() {
                        showStatusMessage('读取文件时出错', 'error');
                    };
                    
                    reader.readAsText(file);
                }
            });
            
            // 保存文件事件
            saveButton.addEventListener('click', function() {
                if (!currentFile) {
                    showStatusMessage('请先选择文件', 'error');
                    return;
                }
                
                try {
                    const content = fileContent.value;
                    const blob = new Blob([content], { type: 'text/plain' });
                    const url = URL.createObjectURL(blob);
                    
                    const a = document.createElement('a');
                    a.href = url;
                    
                    // 生成新文件名：原文件名 + "_edited"
                    const originalName = currentFile.name;
                    const dotIndex = originalName.lastIndexOf('.');
                    let newName;
                    
                    if (dotIndex > 0) {
                        newName = originalName.substring(0, dotIndex) + '_edited' + originalName.substring(dotIndex);
                    } else {
                        newName = originalName + '_edited';
                    }
                    
                    a.download = newName;
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
                    URL.revokeObjectURL(url);
                    
                    showStatusMessage('文件保存成功！', 'success');
                } catch (error) {
                    showStatusMessage('保存文件时出错: ' + error.message, 'error');
                }
            });
            
            // 重置内容事件
            resetButton.addEventListener('click', function() {
                if (currentFile) {
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        fileContent.value = e.target.result;
                        hideStatusMessage();
                    };
                    reader.readAsText(currentFile);
                } else {
                    fileContent.value = '';
                    hideStatusMessage();
                }
            });
            
            // 显示状态消息
            function showStatusMessage(message, type) {
                statusMessage.textContent = message;
                statusMessage.className = 'status-message ' + type;
                statusMessage.style.display = 'block';
                
                // 3秒后自动隐藏成功消息
                if (type === 'success') {
                    setTimeout(hideStatusMessage, 3000);
                }
            }
            
            // 隐藏状态消息
            function hideStatusMessage() {
                statusMessage.style.display = 'none';
            }
            
            // 文本区域内容变化时启用保存按钮
            fileContent.addEventListener('input', function() {
                if (currentFile) {
                    saveButton.disabled = false;
                }
            });
        });
    </script>
</body>
</html>